package com.dap.exception;

import com.dap.commons.Constants.ResponseCode;

/**
 * 平台级异常基类
 */
public class PlatformException extends RuntimeException {
    private static final long serialVersionUID = 4482113391177005660L;

    /** 内部错误码 */
    private String errorCode;

    /** 异常参数 */
    private String[] errorMessageParams;

    /** 异常信息 */
    private String errorMessage;


    // extends RuntimeException
    public PlatformException() {
        super("");
    }


    // extends RuntimeException
    public PlatformException(final Throwable cause) {
        super(cause);

        setErrorCode(ResponseCode.SYS_ERROR);
    }


    // extends RuntimeException
    protected PlatformException(String message, Throwable cause, boolean enableSuppression,
            boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }


    public PlatformException(String errorCode) {
        super(errorCode + ":" + ErrorCodeContainer.getErrorMessage(errorCode));

        this.errorCode = errorCode;
        this.errorMessage = ErrorCodeContainer.getErrorMessage(errorCode);
    }


    public PlatformException(String errorCode, Throwable e) {
        super(errorCode + ":" + ErrorCodeContainer.getErrorMessage(errorCode), e);

        this.errorCode = errorCode;
        this.errorMessage = ErrorCodeContainer.getErrorMessage(errorCode);
    }


    public PlatformException(String errorCode, String... errorMessageParams) {
        super(errorCode + ":" + ErrorCodeContainer.getErrorMessage(errorCode, errorMessageParams));

        this.errorCode = errorCode;
        this.errorMessageParams = errorMessageParams;
        this.errorMessage = ErrorCodeContainer.getErrorMessage(errorCode, errorMessageParams);
    }


    public PlatformException(String errorCode, String[] errorMessageParams, Throwable e) {
        super(errorCode + ":" + ErrorCodeContainer.getErrorMessage(errorCode, errorMessageParams), e);

        this.errorCode = errorCode;
        this.errorMessageParams = errorMessageParams;
        this.errorMessage = ErrorCodeContainer.getErrorMessage(errorCode, errorMessageParams);
    }


    public String getErrorCode() {
        return errorCode;
    }


    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String[] getErrorMessageParams() {
        return errorMessageParams;
    }

    public void setErrorMessageParams(String[] errorMessageParams) {
        this.errorMessageParams = errorMessageParams;
    }

    public String getErrorMessage() {
        return errorMessage;
    }


    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }


    @Override
    public String toString() {
        return String.format("%s :%s:%s", getClass().getName(), errorCode, errorMessage == null ? ""
                : errorMessage);
    }
}
